/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2020 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "usart.h"

#include "gpio.h"

/* USER CODE BEGIN 0 */
#include "beep.h"
#include "ws2812b.h"

uint8_t RxByte;
uint8_t RxCount;
uint8_t RxBuff[RxMax];

extern uint8_t MINIUM_LIGHTNESS;
extern uint8_t DOOR_OPEN_THRESHOLD;
extern uint16_t ACCEL_RH_THRESHOLD;

extern uint8_t colorR, colorG, colorB;
extern uint8_t colorR_Start, colorG_Start, colorB_Start;
extern uint8_t colorR_End, colorG_End, colorB_End;

extern uint8_t lightness;

/* USER CODE END 0 */

UART_HandleTypeDef huart1;

/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}

void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct;
  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspInit 0 */

  /* USER CODE END USART1_MspInit 0 */
    /* USART1 clock enable */
    __HAL_RCC_USART1_CLK_ENABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_9;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_10;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* USER CODE BEGIN USART1_MspInit 1 */

  /* USER CODE END USART1_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==USART1)
  {
  /* USER CODE BEGIN USART1_MspDeInit 0 */

  /* USER CODE END USART1_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_USART1_CLK_DISABLE();

    /**USART1 GPIO Configuration
    PA9     ------> USART1_TX
    PA10     ------> USART1_RX
    */
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);

    /* USART1 interrupt Deinit */
    HAL_NVIC_DisableIRQ(USART1_IRQn);
  /* USER CODE BEGIN USART1_MspDeInit 1 */

  /* USER CODE END USART1_MspDeInit 1 */
  }
}

/* USER CODE BEGIN 1 */

void FLASH_Write_UART(uint8_t offset, uint16_t Flash_Val) {
  uint32_t address = FLASH_ADD + offset * 1024;

  FLASH_EraseInitTypeDef My_Flash;
  HAL_FLASH_Unlock();

  My_Flash.TypeErase = FLASH_TYPEERASE_PAGES;
  My_Flash.PageAddress = address;
  My_Flash.NbPages = 1;

  uint32_t PageError = 0;
  HAL_FLASHEx_Erase(&My_Flash, &PageError);

  HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, Flash_Val);

  HAL_FLASH_Lock();
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  uint8_t i;

  RxBuff[RxCount++] = RxByte;

  if (RxByte == '#' || RxCount == RxMax) {
    //while(HAL_UART_Transmit_IT(&huart1,RxBuff,RxCount)==HAL_OK);

    RxCount = 0;

    if (RxBuff[0] == 'M' && RxBuff[1] == 'L' && RxBuff[2] == 'S') {
      uint16_t minium_lightness_buffer = 0;

      BEEP_Received();

      for (i = 0; i < 3; i++) {
        if (RxBuff[3 + i] == '#') {
          break;
        } else {
          minium_lightness_buffer *= 10;
          minium_lightness_buffer += RxBuff[3 + i] - '0';
        }
      }

      if (minium_lightness_buffer > 200) {
        minium_lightness_buffer = 200;
      }

      MINIUM_LIGHTNESS = minium_lightness_buffer + 0x34;

      FLASH_Write_UART(1, MINIUM_LIGHTNESS);

      lightness = MINIUM_LIGHTNESS;
      WS2812B_On(0xff, 0xff, 0xff);

      HAL_UART_Transmit(&huart1, (uint8_t *)"\r\nMinium Linghtness Set: OK!\r\n", 30, 0x0f);
    } else if (RxBuff[0] == 'D' && RxBuff[1] == 'O' && RxBuff[2] == 'T') {
      uint16_t door_open_buffer = 0;

      BEEP_Received();

      for (i = 0; i < 3; i++) {
        if (RxBuff[3 + i] == '#') {
          break;
        } else {
          door_open_buffer *= 10;
          door_open_buffer += RxBuff[3 + i] - '0';
        }
      }

      if (door_open_buffer < 50) {
        door_open_buffer = 50;
      } else if (door_open_buffer > 250) {
        door_open_buffer = 250;
      }

      DOOR_OPEN_THRESHOLD = door_open_buffer;

      FLASH_Write_UART(2, DOOR_OPEN_THRESHOLD);

      HAL_UART_Transmit(&huart1, (uint8_t *)"\r\nDoor Open Threshold set: OK!\r\n", 32, 0x0f);
    } else if (RxBuff[0] == 'A' && RxBuff[1] == 'R' && RxBuff[2] == 'T') {
      uint16_t accel_rh_threshold = 0;

      BEEP_Received();

      for (i = 0; i < 3; i++) {
        if (RxBuff[3 + i] == '#') {
          break;
        } else {
          accel_rh_threshold *= 10;
          accel_rh_threshold += RxBuff[3 + i] - '0';
        }
      }

      ACCEL_RH_THRESHOLD = accel_rh_threshold + 500;

      FLASH_Write_UART(3, ACCEL_RH_THRESHOLD);

      HAL_UART_Transmit(&huart1, (uint8_t *)"\r\nACCEL RH Threshold set: OK!\r\n", 31, 0x0f);
    } else if (RxBuff[0] == 'L' && RxBuff[1] == 'C' && RxBuff[2] == 'S') {
      uint16_t color_buffer = 0;
      uint8_t pos = 0, pos_buffer = 0;

      BEEP_Received();

      for (i = 0; i < 4; i++) {
        pos++;
        if (RxBuff[3 + i] == ',') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorR = color_buffer;

      pos_buffer = pos;
      color_buffer = 0;

      for (i = 0; i < 4; i++) {
        pos++;
        if (RxBuff[pos_buffer + 3 + i] == ',') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[pos_buffer + 3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorG = color_buffer;

      pos_buffer = pos;
      color_buffer = 0;

      for (i = 0; i < 4; i++) {
        if (RxBuff[pos_buffer + 3 + i] == '#') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[pos_buffer + 3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorB = color_buffer;

      FLASH_Write_UART(4, colorR);
      FLASH_Write_UART(5, colorG);
      FLASH_Write_UART(6, colorB);

      WS2812B_On(colorR, colorG, colorB);

      HAL_UART_Transmit(&huart1, (uint8_t *)"\r\nLight Color Set: OK!\r\n", 24, 0x0f);
    } else if (RxBuff[0] == 'S' && RxBuff[1] == 'C' && RxBuff[2] == 'S') {
      uint16_t color_buffer = 0;
      uint8_t pos = 0, pos_buffer = 0;

      BEEP_Received();

      for (i = 0; i < 4; i++) {
        pos++;
        if (RxBuff[3 + i] == ',') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorR_Start = color_buffer;

      pos_buffer = pos;
      color_buffer = 0;

      for (i = 0; i < 4; i++) {
        pos++;
        if (RxBuff[pos_buffer + 3 + i] == ',') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[pos_buffer + 3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorG_Start = color_buffer;

      pos_buffer = pos;
      color_buffer = 0;

      for (i = 0; i < 4; i++) {
        if (RxBuff[pos_buffer + 3 + i] == '#') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[pos_buffer + 3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorB_Start = color_buffer;

      FLASH_Write_UART(7, colorR_Start);
      FLASH_Write_UART(8, colorG_Start);
      FLASH_Write_UART(9, colorB_Start);

      WS2812B_On(colorR_Start, colorG_Start, colorB_Start);

      HAL_UART_Transmit(&huart1, (uint8_t *)"\r\nStart Color Set: OK!\r\n", 24, 0x0f);
    } else if (RxBuff[0] == 'E' && RxBuff[1] == 'C' && RxBuff[2] == 'S') {
      uint16_t color_buffer = 0;
      uint8_t pos = 0, pos_buffer = 0;

      BEEP_Received();

      for (i = 0; i < 4; i++) {
        pos++;
        if (RxBuff[3 + i] == ',') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorR_End = color_buffer;

      pos_buffer = pos;
      color_buffer = 0;

      for (i = 0; i < 4; i++) {
        pos++;
        if (RxBuff[pos_buffer + 3 + i] == ',') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[pos_buffer + 3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorG_End = color_buffer;

      pos_buffer = pos;
      color_buffer = 0;

      for (i = 0; i < 4; i++) {
        if (RxBuff[pos_buffer + 3 + i] == '#') {
          break;
        } else {
          color_buffer *= 10;
          color_buffer += RxBuff[pos_buffer + 3 + i] - '0';
        }
      }
      if (color_buffer > 255) {
        color_buffer = 255;
      }
      colorB_End = color_buffer;

      FLASH_Write_UART(10, colorR_End);
      FLASH_Write_UART(11, colorG_End);
      FLASH_Write_UART(12, colorB_End);

      WS2812B_On(colorR_End, colorG_End, colorB_End);

      HAL_UART_Transmit(&huart1, (uint8_t *)"\r\nEnd Color Set: OK!\r\n", 22, 0x0f);
    }
  }

  while (HAL_UART_Receive_IT(&huart1, &RxByte, 1) == HAL_OK);
}

/* USER CODE END 1 */

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
